<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" append-to-body :visible.sync="dialogVisible" width="520px" @close='close'>
    <el-form ref="form" :model="record" label-width="200px" label-position="left" :rules="rules" style="margin:0 30px;" class="peerDialogForm">
      <el-form-item label="IP" prop="peer">
        <el-input v-emoji v-model="record.peer" ref="peer" style="width:210px" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('远程AS号')" prop="remote_as">
        <el-input v-emoji v-model="record.remote_as" ref="remote_as" style="width:210px" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('多跳跃点建立连接')" prop="ebgp_multihop">
        <el-radio-group v-model="record.ebgp_multihop" size='small'>
          <el-radio label="on" class="ebgpOn">{{$t('开启')}}</el-radio>
          <el-radio label="off" class="ebgpOff">{{$t('关闭')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('宣告默认路由')" prop="default_originate">
        <el-radio-group v-model="record.default_originate" size='small'>
          <el-radio label="on" class="defaultOn">{{$t('开启')}}</el-radio>
          <el-radio label="off" class="defaultOff">{{$t('关闭')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('备用AS号')" prop="local_as">
        <el-input v-emoji v-model="record.local_as" ref="local_as" style="width:210px" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('接收到的路由去除备用AS号')" prop="no_prepend">
        <el-radio-group v-model="record.no_prepend" size='small'>
          <el-radio label="on" class="noOn">{{$t('开启')}}</el-radio>
          <el-radio label="off" class="noOff">{{$t('关闭')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('发送出去的路由去除本地AS号')" prop="replace_as">
        <el-radio-group v-model="record.replace_as" size='small'>
          <el-radio label="on" class="replaceOn">{{$t('开启')}}</el-radio>
          <el-radio label="off" class="replaceOff">{{$t('关闭')}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="peerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="peerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  addBgpPeer // Bgp添加对等体
} from '@/api/network/route/dynamicRoute/BGP'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'PeerDialog',
  data () {
    var validateIp = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入IP地址')))
      } else if (this.isValidIP(value)) {
        callback()
      } else if (this.isValidIPv6(value)) {
        callback()
      } else {
        callback(new Error(this.$t('请输入正确的IP地址')))
      }
    }
    var validateRemoteAs = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 1 || Number(value) > 4294967295) {
        callback(new Error(this.$t('必须在1-4294967295之间')))
      } else {
        callback()
      }
    }
    var validateLocalAs = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 4294967295) {
        callback(new Error(this.$t('必须在0-4294967295之间')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: '',
        peer: '',
        remote_as: '',
        ebgp_multihop: 'on',
        default_originate: 'off',
        local_as: '0',
        no_prepend: 'off',
        replace_as: 'off'
      },
      recordCopy: {
        id: '',
        peer: '',
        remote_as: '',
        ebgp_multihop: 'on',
        default_originate: 'off',
        local_as: '0',
        no_prepend: 'off',
        replace_as: 'off'
      },
      type: '',
      rules: {
        peer: [{ required: false, validator: validateIp, trigger: 'blur' }],
        remote_as: [{ required: false, validator: validateRemoteAs, trigger: 'blur' }],
        local_as: [{ required: false, validator: validateLocalAs, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (type, id) {
      this.dialogVisible = true
      this.type = type
      this.record.id = id
      this.$nextTick(() => {
        const data = ['peer', 'remote_as', 'local_as']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let formData = JSON.parse(JSON.stringify(this.record))
          if (this.type === '编辑') {
            addBgpPeer(formData).then(res => {
              if (res.status === 0) {
                this.$messages.success(this.$t('执行成功'))
                this.close()
                this.$emit('addPeer', formData)
              }
            })
          } else {
            this.$emit('addPeer', formData)
            this.close()
          }
        }
      })
    },
    close () {
      this.type = ''
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    },
    isValidIP (ip) {
      let reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      return reg.test(ip)
    },
    isValidIPv6 (ip) {
      let reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      return reg.test(ip)
    }
  }
}
</script>
<style lang="less" scoped>
.peerDialogForm {
  .el-switch {
    margin-left: 15px;
    /deep/ .el-switch__core {
      &::before {
        content: "关闭";
        color: #fff;
        position: absolute;
        // font-size: 14px;
        transform: scale(0.85);
        left: 20px;
      }
    }
    &.switch-is-actived {
      /deep/ .el-switch__core {
        &::before {
          content: "开启";
          left: 8px;
          transform: scale(0.85);
        }
      }
    }
  }
}
</style>
